#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node{
	void *entry;
	void *data;
	struct node *next;
} NODE;

NODE *list_create(void *entry, void *data)
{
	NODE *node;
	if(!(node=malloc(sizeof(NODE)))) return NULL;
	node->entry = entry;
	node->data = data;
	node->next = NULL;
	return node;
}

NODE *list_push_end(NODE *node, void *entry, void *data)
{
	NODE *newNode;
	newNode = list_create(entry, data);
	newNode->next = node->next;
	node->next = newNode;
	return newNode;
}

NODE *list_push_front(NODE *list, void *entry, void *data)
{
	NODE *newNode;
	newNode = list_create(entry, data);
	newNode->next = list;
	return newNode;
}

int list_pop(NODE *list, NODE *node)
{
	while(list->next && list->next!=node) list=list->next;
	if(list->next)
	{
		list->next = node->next;
		free(node);
		return 0;
	} else return -1;
}

int list_foreach(NODE *node, int(*func)(void*))
{
	while(node)
	{
		if(func(node->entry)!=0) return -1;
		node = node->next;
	}
	return 0;
}

NODE *list_find(NODE *node, int(*func)(void*, void*), void *entry)
{
	while(node)
	{
		if(func(node->entry, entry)>0) return node;
		node = node->next;
	}
	return NULL;
}

int printstring(void *s)
{
	printf("%s\n", (char *)s);
	return 0;
}

int findstring(void *listentry, void *searchentry)
{
	return strcmp((char*)listentry, (char*)searchentry) ? 0 : 1;
}

/*
void main()
{
	NODE *list, *second, *inserted;
	NODE *match;
	
	//create initial elements of list
	list = list_create((void*)"First", (int*)30);
	second = list_push_end(list, (void*)"Second", (int*)90);
	list_push_end(second, (void*)"Third", (int*)30);
	
	printf("init list:\r\n");
	list_foreach(list, printstring);
	putchar('\n');
	
	//push after
	inserted = list_push_end(second, "After", (int*)20);
	printf("after list_push_end():\n");
	list_foreach(list, printstring);
	putchar('\n');
	
	return;
}
*/